package com.neu.dao.klz;

import com.neu.dao.BaseDao;
import com.neu.util.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class GoodDaoImpl extends BaseDao {
    public int save(List<List<String>> list){
        int row=0;
        String sql="insert into shop.t_good(g_id,k_id,e_id,g_name,g_info,g_key,g_date,g_now," +
                " g_num,g_img)" +
                " values(seq_id.nextval,?,?,?,?,?,?,?,?,?)";
        row=super.executeBatch(sql,list);
        return row;
    }
    public int save(String k_id,String e_id,String g_name,String g_info,String g_key,String g_date ,
                    String g_now,String g_num,String g_img){
        int row=0;
        String sql="insert into shop.t_good(g_id,k_id,e_id,g_name,g_info,g_key,g_date,g_now," +
                " g_num,g_img)" +
                " values(seq_id.nextval,?,?,?,?,?,to_date(?,'yyyy-mm-dd'),?,?,?)";
        System.out.println(sql);
        QueryRunner qr=new QueryRunner(DBUtils.getDataSource());
        try {
            row=qr.update(sql,k_id,e_id,g_name,g_info,g_key,g_date,g_now,g_num,g_img);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row;
    }
    public List<Map<String,Object>> findByGood(){
        List<Map<String,Object>>list=null;
        String sql="select g_id,g_name,g_now,g_num from t_good";
        QueryRunner qr=new QueryRunner(DBUtils.getDataSource());
        try {
            list=qr.query(sql,new MapListHandler());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;

    }
    public int upsave( String g_id, String g_name,String g_now,String g_num){
        int row=0;
        String sql="update t_good set g_name=?,g_now=?,g_num=? where g_id=?";
        System.out.println(g_id);
        System.out.println(g_name);
        System.out.println(g_now);
        System.out.println(g_num);
        QueryRunner qr=new QueryRunner(DBUtils.getDataSource());
        try {
            row=qr.update(sql,g_name,g_now,g_num,g_id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row;
    }
    public int deletesave(String g_id){
        int row=0;
        String sql="delete from t_good where g_id=?";
        QueryRunner qr=new QueryRunner(DBUtils.getDataSource());
        try {
            row=qr.update(sql,g_id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row;
    }
}
